# VideoClipsTab - VideoClipsTab is group of objects that are displayed on video clips tab
# Copyright (C) 2007 Lauri Taimila
# 
# Entertainer is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Entertainer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2008, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import gtk
import pango
import clutter

from frontend.user_event import UserEvent
from frontend.gui.screens.screen import Screen
from frontend.gui.transitions.transition import Transition

from frontend.gui.tabs.tab import Tab
from frontend.gui.widgets.image_menu import ImageMenu
from frontend.gui.widgets.list_indicator import ListIndicator
from frontend.gui.widgets.image_menu_item import ImageMenuItem
from frontend.gui.widgets.rounded_texture import RoundedTexture

class VideoClipsTab(Tab):
    """
    Tab can be used as part of the TabGroup
    
    Tab is a very simple container that contains all the widgets and logic
    of the tab page.
    """
    
    def __init__(self, title, frontend):
        """
        Initialize tab
        @param title: Title of the Tab (string)
        @param frontend: FrontendClient object
        """
        Tab.__init__(self, title)
        self.library = frontend.get_video_library()
        self.frontend = frontend
        self.ui = frontend.getUserInterface()
        self.theme = frontend.getTheme()
        
        if self.library.get_number_of_video_clips() == 0:
           self._create_empty_library_notice()
        else:
            self._create_video_clip_menu()
    
    def can_activate(self):
        """
        Allow if we have some movies indexed.
        """
        if self.library.get_number_of_video_clips() == 0:
            return False
        else:
            return True
            
    def set_active(self, boolean):
        """
        Override method from parent class.
        @param boolean: True to set this tab active, False to set inactive
        """
        Tab.set_active(self, boolean)
        if boolean:
            self._update_clip_info(self.clips_menu.get_current_menuitem().get_userdata())
            self.clips_menu.set_active(True)
            self.clip_title.show()
            self.clip_info.show()
            self.list_indicator.show()
        else:
            self.clips_menu.set_active(False)
            self.clip_title.hide()
            self.clip_info.hide()
            self.list_indicator.hide()
            
    def _create_empty_library_notice(self):
        """
        Create an information box that is displayed if there are no indexed
        movies.
        """
        Tab.show_empty_tab_notice(self, "No video clips available!",
                                  "There are no indexed Video Clips in the Entertainer media library. Please add some folders containing video clips to the Library using the configuration tool.")
    
    def _create_video_clip_menu(self):
        """
        Create a view that is displayed when there is indexed movies in
        the video library.
        """
        #Create movie menu
        self.clips_menu = ImageMenu(self.theme)
        self.clips_menu.set_item_size(self.ui.x(0.233), self.ui.y(0.25))
        self.clips_menu.set_item_spacing(self.ui.y(0.025))
        self.clips_menu.set_visible_column_count(2)
        self.clips_menu.set_row_count(3)
        self.clips_menu.set_orientation(self.clips_menu.VERTICAL)
        
        # Default texture
        default_cover = clutter.Texture()
        buffer = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("default_movie_art"))
        default_cover.set_pixbuf(buffer)
        
        clips = self.library.get_video_clips()
        for clip in clips:
            buffer = gtk.gdk.pixbuf_new_from_file(clip.get_thumbnail_url())
            texture = RoundedTexture(buffer, self.ui.x(0.233), self.ui.y(0.25))
            item = ImageMenuItem(self.ui.x(0.233), self.ui.y(0.25), texture)
            item.set_userdata(clip)
            self.clips_menu.add_actor(item)
        
        self.clips_menu.set_position(self.ui.x(0.15), self.ui.y(0.2))
        self.clips_menu.set_active(False)
        self.clips_menu.show()
        
        if self.frontend.get_configuration().show_effects():
            self.clips_menu.set_animate(True)
        
        self.add(self.clips_menu)
        
        # Create list indicator
        self.list_indicator = ListIndicator(0.2, 0.045, self.theme, ListIndicator.VERTICAL)
        self.list_indicator.set_position(self.ui.x(0.67), self.ui.y(0.76))
        self.list_indicator.set_delimiter(" | ")
        self.list_indicator.set_maximum(self.clips_menu.get_number_of_items())
        self.list_indicator.set_current(1)
        self.add(self.list_indicator)
        
        # Create information labels
        self.clip_title = clutter.Label()
        self.clip_title.set_text(" ")
        self.clip_title.set_font_name(self.theme.getFont() + " bold " + str(self.ui.y(0.042)) + "px")
        self.clip_title.set_color(self.theme.getColor("title"))
        self.clip_title.set_line_wrap(False)
        self.clip_title.set_ellipsize(pango.ELLIPSIZE_END)
        self.clip_title.set_position(self.ui.x(0.15), self.ui.y(0.77))
        self.clip_title.set_width(self.ui.x(0.5))
        self.clip_title.show()
        self.add(self.clip_title)
        
        self.clip_info = clutter.Label()
        self.clip_info.set_text(" ")
        self.clip_info.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.034)) + "px")
        self.clip_info.set_color(self.theme.getColor("subtitle"))
        self.clip_info.set_line_wrap(False)
        self.clip_info.set_ellipsize(pango.ELLIPSIZE_END)
        self.clip_info.set_position(self.ui.x(0.15), self.ui.y(0.85))
        self.clip_info.set_width(self.ui.x(0.5))
        self.clip_info.show()
        self.add(self.clip_info)

    def _update_clip_info(self, clip):
        """
        Update VideoClip information. Updates clutter labels on Clips tab
        @param clip: Currently selected VideoClip (VideoClip object)
        """
        (folder, file) = os.path.split(clip.get_filename())
        self.clip_title.set_text(file)
        self.clip_info.set_text(folder)

    def handle_user_event(self, event):
        """ 
        Handle screen specific user events
        @param event: UserEvent object
        @return: Boolean, Should control be moved back to tab bar from tab
        """
        type = event.get_type()
        
        if type == UserEvent.NAVIGATE_UP:
            if self.clips_menu.get_current_postition() <= 2:
                return True # Move control back to tab bar
            else:
                self.clips_menu.move(self.clips_menu.UP)
                self._update_clip_info(self.clips_menu.get_current_menuitem().get_userdata())
                self.list_indicator.set_current(self.clips_menu.get_current_postition() + 1)
        
        elif type == UserEvent.NAVIGATE_DOWN:
            self.clips_menu.move(self.clips_menu.DOWN)
            self._update_clip_info(self.clips_menu.get_current_menuitem().get_userdata())
            self.list_indicator.set_current(self.clips_menu.get_current_postition() + 1)
        
        elif type == UserEvent.NAVIGATE_LEFT:
            self.clips_menu.move(self.clips_menu.LEFT)
            self._update_clip_info(self.clips_menu.get_current_menuitem().get_userdata())
            self.list_indicator.set_current(self.clips_menu.get_current_postition() + 1)
            
        elif type == UserEvent.NAVIGATE_RIGHT:
            self.clips_menu.move(self.clips_menu.RIGHT)
            self._update_clip_info(self.clips_menu.get_current_menuitem().get_userdata())
            self.list_indicator.set_current(self.clips_menu.get_current_postition() + 1)
            
        elif type == UserEvent.NAVIGATE_SELECT:
            clip = self.clips_menu.get_current_menuitem().get_userdata()
            mp = self.ui.get_media_player()
            mp.set_media(clip)
            mp.play()
            self.ui.changeScreen(self.ui.createScreen("video_osd"), Transition.FORWARD)
        return False
    
